home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / chrome / sync.jar / content / status.js < prev    next >
Text File  |  2008-07-11  |  8KB  |  222 lines

  1. const Cc = Components.classes;
  2. const Ci = Components.interfaces;
  3. const Cr = Components.results;
  4. const Cu = Components.utils;
  5.  
  6. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  7. Cu.import("resource://weave/log4moz.js");
  8. Cu.import("resource://weave/service.js");
  9.  
  10. let WeaveStatus = {
  11.   get _os() {
  12.     delete this._os;
  13.     return this._os = Cc["@mozilla.org/observer-service;1"].
  14.                                        getService(Ci.nsIObserverService);
  15.   },
  16.  
  17.   __enginesCompleted: 0,
  18.   get _enginesCompleted() {
  19.     return this.__enginesCompleted;
  20.   },
  21.   set _enginesCompleted(count) {
  22.     this.__enginesCompleted = count;
  23.   },
  24.  
  25.   get _log() {
  26.     delete this._log;
  27.     return this._log = Log4Moz.Service.getLogger("Sync.Status");
  28.   },
  29.  
  30.   get _stringBundle() {
  31.     delete this._stringBundle;
  32.     return this._stringBundle = document.getElementById("weaveStringBundle");
  33.   },
  34.  
  35.   get _statusDialog() {
  36.     delete this._statusDialog;
  37.     return this._statusDialog = document.getElementById("sync-status-dialog");
  38.   },
  39.  
  40.   get _statusIcon() {
  41.     delete this._statusIcon;
  42.     return this._statusIcon = document.getElementById("statusIcon");
  43.   },
  44.  
  45.   get _statusProgress() {
  46.     delete this._statusProgress;
  47.     return this._statusProgress = document.getElementById("statusProgress");
  48.   },
  49.  
  50.   get _statusEngine() {
  51.     delete this._statusEngine;
  52.     return this._statusEngine = document.getElementById("statusEngine");
  53.   },
  54.  
  55.   get _statusText() {
  56.     delete this._statusText;
  57.     return this._statusText = document.getElementById("statusText");
  58.   },
  59.  
  60.   get _statusTitle() {
  61.     delete this._statusTitle;
  62.     return this._statusTitle = document.getElementById("statusTitle");
  63.   },
  64.  
  65.   get _statusError() {
  66.     delete this._statusError;
  67.     return this._statusError = document.getElementById("statusError");
  68.   },
  69.  
  70.   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
  71.                                          Ci.nsISupportsWeakReference]),
  72.  
  73.   onLoad: function WeaveStatus_onLoad() {
  74.     this._os.addObserver(this, "weave:service:sync:start", true);
  75.     this._os.addObserver(this, "weave:service:sync:engine:start", true);
  76.     this._os.addObserver(this, "weave:service:sync:status", true);
  77.     this._os.addObserver(this, "weave:service:sync:success", true);
  78.     this._os.addObserver(this, "weave:service:sync:error", true);
  79.  
  80.     this._os.addObserver(this, "weave:service:global:success", true);
  81.     this._os.addObserver(this, "weave:service:global:error", true);
  82.  
  83.     // FIXME: we should set a timer to force quit in case there is a
  84.     // stale local lock
  85.     // FIXME: abort any running sync here, once we have support for that
  86.     if (Weave.DAV.locked) {
  87.       this._log.info("Waiting for current sync to finish");
  88.       this._existingSync = true;
  89.       this._statusIcon.setAttribute("status", "active");
  90.       this._statusText.value = this._stringBundle.getString("status.wait");
  91.       this._statusEngine.value = "";
  92.     } else {
  93.       this.doSync();
  94.     }
  95.   },
  96.  
  97.   onUnload: function WeaveStatus_onUnload() {
  98.     this._os.removeObserver(this, "weave:service:sync:start");
  99.     this._os.removeObserver(this, "weave:service:sync:engine:start");
  100.     this._os.removeObserver(this, "weave:service:sync:status");
  101.     this._os.removeObserver(this, "weave:service:sync:success");
  102.     this._os.removeObserver(this, "weave:service:sync:error");
  103.  
  104.     this._os.removeObserver(this, "weave:service:global:success");
  105.     this._os.removeObserver(this, "weave:service:global:error");
  106.   },
  107.  
  108.   doCancel: function WeaveStatus_doCancel() {
  109.     this._statusText.value = this._stringBundle.getString("status.cancel");
  110.     this._statusDialog.getButton("cancel").setAttribute("disabled", "true");
  111.     Weave.Service.cancelRequested = true;
  112.     return false;
  113.   },
  114.  
  115.   doSync: function WeaveStatus_doSync() {
  116.     try {
  117.       // XXX Should we set a timeout to cancel sync if it takes too long?
  118.       if(Weave.Service.isInitialized && !Weave.Service.isQuitting) {
  119.         Weave.Service.sync();
  120.       } else if(Weave.Service.isInitialized && Weave.Service.isQuitting &&
  121.                 Weave.Utils.prefs.getBoolPref("syncOnQuit.enabled")) {
  122.         Weave.Service.sync();
  123.       } else {
  124.         this._log.info("Skipping modal sync");
  125.         window.close();
  126.       }
  127.     }
  128.     catch(ex) {
  129.       this._log.error("Error starting modal sync: " + ex);
  130.       window.close();
  131.     }
  132.   },
  133.  
  134.   // nsIObserver
  135.  
  136.   observe: function WeaveSync__observe(subject, topic, data) {
  137.     switch (topic) {
  138.  
  139.     case "weave:service:sync:start":
  140.     this._statusIcon.setAttribute("status", "active");
  141.     break;
  142.  
  143.     case "weave:service:sync:status":
  144.     if(!Weave.Service.cancelRequested)
  145.       this._statusText.value = this._stringBundle.getString(data);
  146.     break;
  147.  
  148.     case "weave:service:sync:engine:start":
  149.     this._statusText.value = this._stringBundle.getString("status.engine_start");
  150.     this._statusEngine.value = data;
  151.     this._enginesCompleted++;
  152.     this._statusProgress.value = this._enginesCompleted / (Weave.Engines.getEnabled().length + 1) * 100;
  153.     break;
  154.  
  155.     case "weave:service:sync:success":
  156.     if (this._existingSync)
  157.       break;
  158.  
  159.     this._statusDialog.getButton("cancel").setAttribute("disabled", "true");
  160.  
  161.     if (Weave.Service.cancelRequested) {
  162.       Weave.Service.cancelRequested = false;
  163.       this._statusIcon.setAttribute("status", "cancelled");
  164.       this._statusText.value = this._stringBundle.getString("status.cancelled");
  165.     } else {
  166.       this._statusIcon.setAttribute("status", "success");
  167.       this._statusProgress.value = "100";
  168.       this._statusEngine.value = this._stringBundle.getString("status.success");
  169.       this._statusEngine.style.color = "blue";
  170.       this._statusText.value = this._stringBundle.getString("status.closing");
  171.     }
  172.     // Delay closing the window for a couple seconds to give the user time
  173.     // to see the result of the sync.
  174.     window.setTimeout(window.close, 2000);
  175.  
  176.     // FIXME: send a growl or other low-priority notification.
  177.     break;
  178.  
  179.     case "weave:service:sync:error":
  180.     if (this._existingSync)
  181.       break;
  182.  
  183.     this._statusDialog.getButton("cancel").setAttribute("disabled", "true");
  184.  
  185.     if (Weave.Service.cancelRequested)
  186.       Weave.Service.cancelRequested = false;
  187.  
  188.     if (Weave.FaultTolerance.Service.lastException == "Could not acquire lock") {
  189.       this._statusIcon.setAttribute("status", "info");
  190.       this._statusEngine.value = this._stringBundle.getString("status.locked");
  191.       if (Weave.Service.isQuitting)
  192.         this._statusText.value = this._stringBundle.getString("status.closing");
  193.       else
  194.         this._statusText.value = this._stringBundle.getString("status.tryagain");
  195.     } else {
  196.       this._statusIcon.setAttribute("status", "error");
  197.       this._statusEngine.value = this._stringBundle.getString("status.error") + " (" + Weave.FaultTolerance.Service.lastException + ")";
  198.       this._statusText.value = this._stringBundle.getString("status.closing");
  199.       this._statusEngine.style.color = "red";
  200.     }
  201.  
  202.     // Delay closing the window for a couple seconds to give the user time
  203.     // to see the result of the sync.
  204.     window.setTimeout(window.close, 2000);
  205.  
  206.     // FIXME: send a growl or other low-priority notification, or don't exit
  207.     // and let the user try again.
  208.     break;
  209.  
  210.     case "weave:service:global:success":
  211.     case "weave:service:global:error":
  212.     if (this._existingSync && !Weave.DAV.locked) {
  213.       this._log.info("Existing action finished, starting modal sync.");
  214.       this._existingSync = false;
  215.       this.doSync();
  216.     }
  217.     break;
  218.     }
  219.   }
  220.  
  221. };
  222.